package com.ss.android.push.sswo;

import android.content.Context;
import android.content.IntentFilter;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.PowerManager;
import android.view.Display;

import com.bytedance.common.plugin.interfaces.pushmanager.setting.PushSetting;

public class SswoManager {
    private static volatile SswoManager c;
    private Context a;
    private SswoReceiver b;

    private SswoManager(Context arg2) {

        this.a = arg2.getApplicationContext();
        this.b = new SswoReceiver();
    }

    public static SswoManager inst(Context arg2) {
        if (SswoManager.c == null) {
            Class v1 = SswoManager.class;
            synchronized (v1) {
                if (SswoManager.c == null) {
                    SswoManager.c = new SswoManager(arg2);
                }
            }
        }

        return SswoManager.c;
    }

    public void destroyOffActivity() {
        try {
            SswoActivity.finishOffActivity(this.a);
        } catch (Throwable v0) {
        }
    }

    public boolean isScreenOff() {
        try {
            if (Build.VERSION.SDK_INT >= 20) {
                Display[] v0_1 = ((DisplayManager) this.a.getSystemService(Context.DISPLAY_SERVICE)).getDisplays();
                if (v0_1 == null) {
                    return false;
                }

                if (v0_1.length <= 0) {
                    return false;
                }

                if (1 != v0_1[0].getState()) {
                    return false;
                }

                boolean v0_2 = true;
                return v0_2;
            }

            if (((PowerManager) this.a.getSystemService(Context.POWER_SERVICE)).isScreenOn()) {
                return false;
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
            return false;
        }

        return true;
    }

    public void registerReceiver() {
        try {
            if (!PushSetting.getInstance().isAllowOffAlive()) {
                this.unRegisterReceiver();
                return;
            }

            IntentFilter v0_1 = new IntentFilter();
            v0_1.addAction("android.intent.action.SCREEN_OFF");
            v0_1.addAction("android.intent.action.SCREEN_ON");
            v0_1.addAction("android.intent.action.USER_PRESENT");
            this.a.registerReceiver(this.b, v0_1);
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    public void startOffActivity() {
        try {
            if (!PushSetting.getInstance().isAllowOffAlive()) {
                this.destroyOffActivity();
                return;
            }

            SswoActivity.startOffActivity(this.a);
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    public void unRegisterReceiver() {
        try {
            this.a.unregisterReceiver(this.b);
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }
}

